<?php
class Comment_model extends MY_Model
{
	public
		$table = 'blog_comments',
		$prKey = 'bc_id',
		$publishedField = 'status',
		$title = 'Comments Manager',

		$status = array(
			'1' => 'Active',
			'2' => 'Blocked',
		),

		$category = array(
			'1' => 'Comment',
			'2' => 'News Pages',
		)
	;

	function __construct()
	{
		parent::__construct();

		$this->fields = array(
			'id' 				=> '',
			'date_created'		=> T_('Date Creation'),
			'author'			=> T_('Author'),
			'status' 			=> T_('Status'),
			'under_post' 		=> T_('Under Post'),
			'text' 				=> T_('Comment'),
			'email'				=> T_('Email'),
			'captcha_id' 		=> T_('ID Captcha'),
			'captcha_code' 		=> T_('Code verification'),
		);
			
		$this->rules = array(
			'id' 				=> "trim|integer",
			'text' 				=> "trim|required",
			'date_created'		=> "trim|max_length[255]",
			'under_post'		=> "trim|required|max_length[255]",
			'email'				=> "trim|valid_email|max_length[64]",
			'author' 			=> "trim|required|max_length[255]",
			'status' 			=> "trim|required|integer",
			'captcha_id' 		=> "trim|required",
			'captcha_code' 		=> "trim|required|callback___captcha",
		);
	}
	
	function search($postId = null) {
   		$sql = "SELECT *
	   			FROM ".$this->table;
   		if($postId != null)	{ 
	   		$sql .= "WHERE bc_post_ID=".$postId;
   		}
   		$this->data['list'] = $this->db->query($sql)->result();
		//$this->data['search'] = $search;	
   	}	
	
	function _search($config = array())
	{
		$order 		= $this->input->get_post('order');
		$direction 	= $this->input->get_post('direction');
		$search 	= $this->input->get_post('search');

		$sql = '';
		$sql .= ' FROM '.$this->table.' p';
		$sql .= ' LEFT JOIN admins a ON p.id_admins=a.id';
		$sql .= ' WHERE 1 = 1 AND p.category='.$this->id_category; 
								
		if ($search)
		{
			$sql .= ' AND p.title LIKE ' . $this->db->escape('%' . $search . '%'); 
		}
		
		switch ($order)
		{
			case 'date':
				$sql .= ' ORDER by p.page_created'; 
				break;
			
			default:
				$sql .= ' ORDER BY p.id';
				$direction = 'DESC';
		}
		
		$sql .= ($direction == 'DESC') ? ' DESC' : ' ASC';

		$sql = 'SELECT p.*, CONCAT(a.first_name, " ", a.last_name) AS admin_name' . $sql; 

		$this->data['list'] = $this->db->query($sql)->result();
		$this->data['search'] = $search;
	}

	function get($id = null)
	{	
		$this->db->from($this->table);
		//$this->db->join('admins', $this->table.'.id_admins = admins.id', 'left');
		$this->db->where($this->table.'.bc_id', $id);
		$query = $this->db->get();

		return $query->row_array();
	}
	
	function getByAlias($alias = 'all')
	{
		$this->db->select('*');
		$this->db->from('pages');
		$this->db->where('category', $this->id_category);
		$this->db->where('status', 1);
		
		if ($alias != 'all')
		{
			$this->db->where('alias', $alias);
			return $this->db->get()->row();
		}
		else
		{
			$this->db->order_by('date_created', 'desc'); 	
			return $this->db->get()->result();
		}
	}		

	function getNewsBlock($category = PAGE_CATEGORY_ADVERTISER)
	{
		$this->db->select('*');
		$this->db->from('pages');
		$this->db->where('category', $category);
		$this->db->where('status', 1);
		$this->db->limit(3);
		$this->db->order_by('date_created', 'desc'); 				

		return $this->db->get()->result();
	}		
	
	function delete($items_serialized = null)
	{
		$this->log->writeMessage('done', $items_serialized);
		$items = explode("&", $items_serialized);
		
		$error = "";
		$report = "";
		foreach ($items as $id)
		{	
			$data = $this->comment_model->get($id);
			
			$res = $this->db->delete($this->table, array($this->prKey => $id));
			
			if (is_null($res))
			{
				$this->log->writeMessage('error', T_('Error. Comment was not removed.'));
				$report .= ",0"; // don't deleted
			}
			else
			{
				$this->log->writeMessage('done', T_('Comment was removed.'));
				$report .= ",1"; // deleted								
			}							
		}
		
		return substr($report, 1);
		
	}
		
	function insert($data)
	{
		if (!empty($data))
		{
			//$data['date_created'] = MYSQL_NOW();
			
			parent::insert($data);

			if ($this->db->insert_id() > 0)
			{
				$this->log->writeMessage('done', T_('Page was added succesfully.'));
				
				return TRUE;
			}
			else
			{
				$this->log->writeMessage('error', T_('Error! Page was not created.'));
				
				return FALSE;
			}
		}		
	}

	function update($data)
	{
		if (!empty($data))
		{
			$res = parent::update($data);

			$this->log->writeMessage('done', T_('Saved succesfully.'));

			return TRUE;			
		}		
	}
	function getTitle($id)
	{
		$sql = "SELECT *
				FROM blog_records
				WHERE br_id=".$id;
		
		$result = $this->db->query($sql)->row_array();
		
		return $result['br_title'];
	}
}
?>